package com.mobi.Survey.lib.activities;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.mobi.Survey.lib.R;
import com.mobi.Survey.lib.model.AlarmPojo;

public class CustomAdapterTimerList extends BaseAdapter {

	private LayoutInflater mInflater;
	private Context ctx;
	private List<AlarmPojo> alarms;
	private Set<AlarmPojo> selectedAlarms = new LinkedHashSet<AlarmPojo>();

	public Set<AlarmPojo> getSelectedAlarms() {
		return selectedAlarms;
	}

	private SimpleDateFormat startFormat = new SimpleDateFormat("dd MMM yyyy mm:hh:ss");
	private SimpleDateFormat endFormat = new SimpleDateFormat("dd MMM yyyy");

	public CustomAdapterTimerList(Context ctx, List<AlarmPojo> alarms) {

		this.ctx = ctx;
		this.alarms = alarms;
		this.mInflater = LayoutInflater.from(ctx);

	}

	@Override
	public int getCount() {

		return this.alarms.size();
	}

	public boolean removeItem(Object obj) {
		return alarms.remove(obj);
	}

	@Override
	public Object getItem(int position) {

		return alarms.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return alarms.get(position).getId();
	}

	static final class ViewHolder {

		TextView tvMessage;
		TextView tvStartDate;
		TextView tvEndDate;
		TextView tvRepeat;
		// TextView tvSerialNo;

	}

	private boolean selectAll;

	public void toggleSelectAll(boolean select) {
		selectAll = select;
		this.notifyDataSetChanged();
		if (!select) {
			this.selectedAlarms.clear();
		} else {
			this.selectedAlarms.addAll(alarms);
		}

	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		AlarmPojo alarm = alarms.get(position);
		final ViewHolder holder;
		if (convertView == null) {

			holder = new ViewHolder();

			convertView = mInflater.inflate(R.layout.timer_list_row, null);

			holder.tvMessage = (TextView) convertView.findViewById(R.id.tvMessage);
			holder.tvStartDate = (TextView) convertView.findViewById(R.id.tvStartDate);
			holder.tvEndDate = (TextView) convertView.findViewById(R.id.tvEndDate);
			holder.tvRepeat = (TextView) convertView.findViewById(R.id.tvRepeat);
			// holder.tvSerialNo =
			// (TextView)convertView.findViewById(R.id.tvSerialNo);

			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();

		}

		// holder.tvSerialNo.setText(position+1+"");

		Calendar mCalendar = Calendar.getInstance();
		mCalendar.setTimeInMillis(alarm.getStart());
		// 1409545321000
		holder.tvMessage.setText(alarm.getMessage());
		long displayTime = alarm.calculateWrtInstallTime();
		if(displayTime == 0){
			displayTime = alarm.getStart();
		}
			
			
		holder.tvStartDate.setText(new Date(displayTime).toLocaleString());
		holder.tvEndDate.setText(endFormat.format(new Date(alarm.getEnd())));
		holder.tvRepeat.setText("Repeat: " + alarm.getRepeatText());

		convertView.setBackgroundDrawable(ctx.getResources().getDrawable(R.drawable.item_bg));

		if (convertView.getBackground() == null && selectAll) {
			convertView.setBackgroundDrawable(ctx.getResources().getDrawable(R.drawable.item_bg));

		} else

		if (this.selectedAlarms.contains(this.getItem(position))) {
			convertView.setBackgroundDrawable(ctx.getResources().getDrawable(R.drawable.item_bg));
		} else {

			convertView.setBackgroundDrawable(null);
		}

		return convertView;

	}

}
